package com.zjbti.note;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Instrumentation;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.DialogPreference;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zjbti.note.adapter.NoteAdapter;
import com.zjbti.note.bean.NoteBean;
import com.zjbti.note.utils.DBUtils;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<NoteBean> list;
    private DBUtils dbUtils;
    private NoteAdapter adapter;
    private ActivityResultLauncher launcher;
    private RecyclerView rv_list;
    private ImageView iv_add;
    private TextView tv_main_title;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initLauncher();
        init();
    }
    private void initLauncher(){
        launcher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            public void onActivityResult(ActivityResult result) {
                         int resultCode=result.getResultCode();
                         if (resultCode==2) {
                             showQueryData();
                             }
                         }
  });
    }
    private void init() {
        dbUtils=new DBUtils(this);
        tv_main_title=findViewById(R.id.tv_main_title);
        tv_main_title.setText("记事本");
        rv_list=findViewById(R.id.rv_list);
        iv_add=findViewById(R.id.iv_add);
        rv_list.setLayoutManager(new LinearLayoutManager(this));
        adapter=new NoteAdapter(this, new NoteAdapter.ItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {

            }

            @Override
            public void onItemLongClick(View v, int position) {
                AlertDialog dialog;
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.drawable.iv_del)
                        .setTitle("删除提示")
                        .setMessage("是否删除记录？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                NoteBean noteBean = list.get(position);
                                if (dbUtils.deleteNote(noteBean.getId())) {
                                    list.remove(position);
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setNegativeButton("取消",new DialogInterface.OnClickListener(){
                            public  void onClick(DialogInterface dialog,int which){
                                dialog.dismiss();
                            }
                        });
                dialog=builder.create();
                dialog.show();
            }
        });
    }
    private void showQueryData(){
        if(list!=null){
            list.clear();
        }
        list=dbUtils.queryNote();
        adapter.setData(list);
        rv_list.setAdapter(adapter);
    }
}